import { Effect , Reducer, Subscription } from 'umi';
import { GetTagData } from "@/services";
import { tag_item } from '@/utils/tag'

export interface IndexModelState {
    name: string;
    page: number;
    tagArr : tag_item[];
    tagSum : number[];
}

export interface IndexModelType {
    namespace: 'tag';
    state: IndexModelState;
    effects: {
        getTagData : Effect;
        GetSum : Effect;
    };
    reducers: {
        save: Reducer<IndexModelState>;
        // 启用 immer 之后
        // save: ImmerReducer<IndexModelState>;
    };
    subscriptions?: { setup: Subscription };
}

const AboutModel: IndexModelType = {
    namespace: 'tag',

    state: {
        name: '',
        page: 1,
        tagArr : [],
        tagSum: []
    },

    effects: {

        // 获取数字
        *GetSum({ payload } , { call , put }){

            let NewSum :any = []

            for( let i = 0 ; i < payload.data.length ; i ++ ){
                yield GetTagData(payload.data[i]).then(val=>{

                    NewSum.push(val.data[0].length)
                })

            }

            yield put({
                type : 'save',
                payload: {
                    tagSum:NewSum
                }
            })
        },

        // 获取数据
        *getTagData({ payload }, { call, put }) {
            
            let res =yield call(GetTagData , payload.data );

            yield put({
                type: "save",
                payload: {
                    tagArr: res.data[0]
                }
            })
        },


    },
    reducers: {
        save(state, action) {
            return {
                ...state,
                ...action.payload,
            };
        },
        // 启用 immer 之后
        // save(state, action) {
        //   state.name = action.payload;
        // },
    },

};

export default AboutModel;